package com.zx.kitchen.index.common.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.activity.IndexActivity;
import com.zx.kitchen.index.common.activity.ReportGoodsDetailActivity;
import com.zx.kitchen.index.common.adapter.GoodsReportDishListAdapter;
import com.zx.kitchen.index.common.entity.CateListBean;
import com.zx.kitchen.index.common.entity.GoodReportGetDishesByFidBean;
import com.zx.kitchen.index.common.entity.GoodsReportDishesBean;
import com.zx.kitchen.index.common.mvp.presenter.BusinessGoodsReportGetDishesByFidPresenter;
import com.zx.kitchen.index.common.mvp.presenter.PurchaseGoodsReportGetDishesByFidPresenter;
import com.zx.kitchen.index.common.mvp.view.GoodsReportGetDishesByFidView;
import com.zx.kitchen.index.common.utils.Toasts;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/5.
 * 要区分营业还是采购
 */

public class GoodReportDishListFragment extends Fragment implements GoodsReportGetDishesByFidView {
    private ListView listView;
    private List<GoodsReportDishesBean> mList = new ArrayList<>();

    private GoodsReportDishListAdapter adapter;
    private Context context;

    private TextView tvSelectAll;
    private BusinessGoodsReportGetDishesByFidPresenter businessGoodsReportGetDishesByFidPresenter;
    private PurchaseGoodsReportGetDishesByFidPresenter purchaseGoodsReportGetDishesByFidPresenter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_catelist, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();

        tvSelectAll = (TextView) view.findViewById(R.id.tv_select_all);
        listView = (ListView) view.findViewById(R.id.catelist);
        adapter = new GoodsReportDishListAdapter(context, mList);
        tvSelectAll.setVisibility(View.GONE);
        businessGoodsReportGetDishesByFidPresenter = new BusinessGoodsReportGetDishesByFidPresenter(this, context);
        purchaseGoodsReportGetDishesByFidPresenter = new PurchaseGoodsReportGetDishesByFidPresenter(this, context);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getActivity(), ReportGoodsDetailActivity.class)
                        .putExtra("goods_id", mList.get(position).getGoods_id())
                        .putExtra("goods_name", mList.get(position).getName()));
            }
        });

    }


    //当左边订单栏目被修改的时候右边数据相应修改
    public void setData(boolean isPurchase, CateListBean cateListBean) {
        if (isPurchase) {
            purchaseGoodsReportGetDishesByFidPresenter.getFromNet(cateListBean.getFid(), cateListBean.getIs_last());
        } else {
            businessGoodsReportGetDishesByFidPresenter.getFromNet(cateListBean.getFid(), cateListBean.getIs_last());
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onSuccess(GoodReportGetDishesByFidBean goodReportGetDishesByFidBean) {
        mList.clear();
        mList.addAll(goodReportGetDishesByFidBean.getDishes_list());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }

}
